/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.buzzwords.resources;

import com.buzzwords.dataaccess.Database;
import com.buzzwords.exceptions.DuplicateEntryException;
import com.buzzwords.factories.BuzzwordDatabaseFactory;
import com.buzzwords.factories.DatabaseFactory;
import com.buzzwords.domain.Buzzword;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import net.sf.json.JSONObject;

/**
 *
 * @author potlow
 */
@Path("/buzzword")
public class BuzzwordResource {

    @Path("/add/")
    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Produces("text/plain")
    public String createBuzzword(@FormParam("name") String name, @FormParam("definition") String definition, @FormParam("userid") Integer userid) {

        Buzzword buzzword = new Buzzword();
        buzzword.setWord(name);
        buzzword.setDefinition(definition);
        buzzword.setUserId(userid);

        DatabaseFactory databaseFactory = new BuzzwordDatabaseFactory();
        Database database = databaseFactory.getDatabase("Buzzword ID");
        try {
            if (database.performCreate(buzzword)) {
                return "ok";
            } else {
                return "not ok";
            }
        } catch (DuplicateEntryException ex) {
            Logger.getLogger(BuzzwordResource.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "not ok";
    }
}
